import request from '@/utils/request'

export interface postManagementVO {
  id: number;
  processInstanceId: string;
  title: string;
  isPublic: boolean;
  ledgerSequence: string;
  mainText: string;
  signatory: string;
  draftingDepartment: string; // 拟稿部门
  draftingAuthor: string; // 拟稿人
  coSign: string;
  departmentReview: string;
  officeReview: string;
  mainRecipient: string;
  ccReport: string;
  ccSend: string;
  startUserSelectAssignees: any;
  subjectKeywords: string;
  documentNumber: string;
  proofreader: string;
  totalPrinted: number;
  remark: string;
  status: number;
  isRead: boolean;
  isApproved: any;
  eids:any;
  ename:any;
  type: any;
  [key: string]: any
}

// 青运集团所属企业发文审批 API
export const postManagementApi = {
  // 查询青运集团所属企业发文审批分页
  getOutGoingPage: async (params: any) => {
    return await request.get({ url: '/bpm/sendout-article/page', params })
  },

  // 查询青运集团所属企业发文审批详情
  getOutGoing: async (id: number) => {
    return await request.get({ url: '/bpm/sendout-article/get?id=' + id })
  },

  // 新增青运集团所属企业发文审批
  createOutGoing: async (data: postManagementVO) => {
    return await request.post({ url: '/bpm/sendout-article/create', data: data })
  },

  // 修改青运集团所属企业发文审批
  updateOutGoing: async (data: postManagementVO) => {
    return await request.put({ url: `/bpm/sendout-article/update`, data })
  },

  // 删除青运集团所属企业发文审批
  deleteOutGoing: async (id: number) => {
    return await request.delete({ url: '/bpm/sendout-article/delete?id=' + id })
  },
  //更新发文个别内容 不受到流程限制
  deleteSendoutArticle: async (data: any) => {
    return await request.put({ url: `/bpm/sendout-article/update-main-text`, data })
  },
  
  // 导出青运集团所属企业发文审批 Excel
  exportOutGoing: async (id: number) => {
    return await request.get({ url: '/bpm/sendout-article/export-excel?id=', id })
  },
}


// 获得角色分表
export const getUserList = async (params: any) => {
  return await request.get({ url: '/system/user/page', params })
}

// 创建发文
export const createOutGoing = async (data: postManagementVO) => {
  return await request.post({ url: '/bpm/sendout-article/create', data: data })
}
// 修改发文管理
export const updateSendoutArticle = async (data: postManagementVO) => {
  return await request.put({ url: `/bpm/sendout-article/update`, data })
}
// 更新发文个别内容 不受到流程限制
export const deleteSendoutArticle= async (data: any) => {
  return await request.put({ url: `/bpm/sendout-article/update-main-text`, data })
}
// 获取发文分页
export const getOutGoingPage = async (params: any) => {
  return await request.get({ url: '/bpm/sendout-article/page', params })
}

// 获得发文详情
export const getOutGoing = async (id: number) => {
  return await request.get({ url: '/bpm/sendout-article/get?id=' + id })
}

//导出Excel
export const exportOutGoing = async (id: number) => {
  return await request.get({ url: '/bpm/sendout-article/export-excel?id=', id })
}

//删除发文
export const deleteOutGoing = async (id: number) => {
  return await request.delete({ url: '/bpm/sendout-article/delete?id=' + id })
}
//年份历史History
export const getOutGoingYear = async () => {
  return await request.get({ url: '/bpm/sendout-article/get-history-list' })
}
//年份历史History
export const getOutGoingHistoryByYear = async (params: any) => {
  return await request.get({ url: '/bpm/sendout-article/get-page-by-year', params })
}

// 抄送 VO
export interface OutCopyArticleVO {
  creator: number[] // 抄送人
  taskId: number // 任务编号
}

export const OutCarBonApi = {
  // 新增抄送
  createCopyArticle: async (data: OutCopyArticleVO) => {
    return await request.post({ url: `/bpm/process-instance/copy/create`, data })
  },
  // 查询抄送分页
  getCopyArticlePage: async (params: any) => {
    return await request.get({ url: `/bpm/process-instance/copy/receive-sendout-copy-page`, params })
  },

  // 查询抄送详情
  getCopyArticle: async (id: number) => {
    return await request.get({ url: `/system/carbon-copy/get?id=` + id })
  },

  // 查询我发起的抄送分页
  getMyCopyArticlePage: async (params: any) => {
    return await request.get({ url: `/bpm/process-instance/copy/send-sendout-copy-page`, params })
  },

  // 已读抄送
  markCopyArticleRead: async (id: any) => {
    // 检查id是否为数组或逗号分隔的字符串
    let params = '';
    if (Array.isArray(id)) {
      // 如果是数组，直接处理
      params = id.map(item => `id=${item}`).join('&');
    } else if (typeof id === 'string' && id.includes(',')) {
      // 如果是逗号分隔的字符串，先分割后处理
      params = id.split(',').map(item => `id=${encodeURIComponent(item.trim())}`).join('&');
    } else {
      // 单个id的情况
      params = `id=${encodeURIComponent(id)}`;
    }

    return await request.put({ url: `/bpm/process-instance/copy/read?${params}` });
  }
}